var config = require('../../utils/config.js');
var netutil = require('../../utils/netutil.js');
var baseutil = require('../../utils/baseutil.js');
var payutil = require('../../utils/LCPayWorking.js');
let { WeToast } = require('../../src/wetoast.js');
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    OrderNo: '',//订单号
    OrderType: '',//订单类别(对应枚举：OrderType:1、国内机票，2、国际机票，3、火车票) 
    OrderDetail: null,//订单详情数据,
    showTicketDetail: false,//是否显示机票订单明细
    showTrainDetail: false,//是否显示火车票明细
    isShowAgreeDialog: false,//是否显示审批同意的dialog
    isShowRefuseDialog: false,//是否显示审批拒绝的dialog
    currentOperatePosition: -1,//当前操作的是第几条数据
    refuseReason: '',//拒绝原因,
    isFrom: 0,//1表示订单  0表示审批
    IsNeedFlightApprove: false,//国内机票是否需要审批
    IsNeedTrainApprove: false,//火车票是否需要审批
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var OrderNo = options.OrderNo
    var OrderType = options.OrderType
    baseutil.log(OrderNo + "---" + OrderType);
    this.setData({
      OrderNo: OrderNo,
      OrderType: OrderType,
      isFrom: options.isFrom,
      IsNeedFlightApprove: app.globalData.TUserInfo.IsNeedFlightApprove,//国内机票是否需要审批
      IsNeedTrainApprove: app.globalData.TUserInfo.IsNeedTrainApprove,//火车票是否需要审批
    })
    wx.setNavigationBarTitle({ title: this.data.isFrom == 1 ? "订单详情" : "审批详情", });
    new app.WeToast()
    this.getOrderData();
  },
  getOrderData() {
    if (this.data.OrderType == 1) {
      this.getTicketOrderDetail()
    } else if (this.data.OrderType == 3) {
      this.getTrainOrderDetail()
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getTicketOrderDetail: function () {
    var THIS = this
    var url = config.getBaseUrl("FlightApi", "GetOrderDetail");

    var Data = new Object();
    Data.OrderNo = this.data.OrderNo

    baseutil.log(url);
    netutil.post(
      url, Data,
      function (res) {
        baseutil.log(res.data);
        if (res.data.ResponseMetaInfo.StatusCode == '0') {
          var tempData = res.data.Data
          var AirLines = tempData.AirLines
          for (var i = 0; i < AirLines.length; i++) {
            var tempAirlines = AirLines[i]
            tempAirlines.BeginDate = tempAirlines.BeginDate.slice(5, tempAirlines.BeginDate.length - 3).
              replace("T", ' ').replace("/", "-");
            tempAirlines.ArriveDate = tempAirlines.ArriveDate.slice(5, tempAirlines.ArriveDate.length - 3).
              replace("T", ' ').replace("/", "-");
            AirLines[i] = tempAirlines
          }
          tempData.AirLines = AirLines

          var TravelApproveProcess = tempData.TravelApproveOrder.TravelApproveProcess
          for (var i = 0; i < TravelApproveProcess.length; i++) {
            var tempTravelApproveProcess = TravelApproveProcess[i]
            tempTravelApproveProcess.ApproveTime = (tempTravelApproveProcess.ApproveTime == null
              || tempTravelApproveProcess.ApproveTime == '') ? "" :
              tempTravelApproveProcess.ApproveTime.slice(5, tempTravelApproveProcess.ApproveTime.length - 3).
                replace("T", ' ').replace("/", "-");
            tempTravelApproveProcess.XingShi = tempTravelApproveProcess.TrueName.substr(0, 1);
            if (tempTravelApproveProcess.ApproveResult == 0) {
              tempTravelApproveProcess.StateText = "待审批";
              tempTravelApproveProcess.curr = true;

            } else if (tempTravelApproveProcess.ApproveResult == 1) {
              tempTravelApproveProcess.StateText = "审批通过";
              tempTravelApproveProcess.curr = false;
            } else {
              tempTravelApproveProcess.StateText = "审批拒绝";
              tempTravelApproveProcess.curr = false;
            }
            TravelApproveProcess[i] = tempTravelApproveProcess
          }
          tempData.TravelApproveOrder.TravelApproveProcess = TravelApproveProcess

          var curUserId = app.globalData.TUserInfo.UserID
          tempData.OrderBase.isShowBottomBtn = false
          for (var i = 0; i < TravelApproveProcess.length; i++) {
            var tempTravelApproveProcess = TravelApproveProcess[i]
            if (tempTravelApproveProcess.ApproveResult == 0) {
              tempData.OrderBase.isShowBottomBtn = (tempTravelApproveProcess.UserID == curUserId);
              baseutil.log(tempTravelApproveProcess.UserID + "---" + curUserId + "---" + tempData.OrderBase.isShowBottomBtn)
              if (tempData.OrderBase.isShowBottomBtn) {
                break;
              }
            }
          }

          var Passengers = tempData.Passengers
          for (var i = 0; i < Passengers.length; i++) {
            var tempPassengers = Passengers[i]
            tempPassengers.cardText = baseutil.getCardText(tempPassengers.CardType)
            Passengers[i] = tempPassengers
          }
          tempData.Passengers = Passengers

          tempData.OrderBase.serviceFee = parseFloat(tempData.OrderBase.SingleOASerFee) + parseFloat(tempData.OrderBase.SingleCitySecSerFee)
          tempData.OrderBase.jijianRanyouText = parseFloat(tempData.Passengers[0].Tax) + parseFloat(tempData.Passengers[0].Oil)
          tempData.OrderBase.isShowBottomBtn = (tempData.OrderBase.VerifyStatus == 1);

          tempData.OrderBase.isShowPayButton = tempData.OrderBase.PayStatus == 0 && tempData.OrderBase.OrderStatus == 1 && (tempData.OrderBase.VerifyStatus == 0 || tempData.OrderBase.VerifyStatus == 2)

          tempData.OrderBase.OrderTime = tempData.OrderBase.OrderTime.slice(5, tempData.OrderBase.OrderTime.length - 3).
            replace("T", ' ').replace("/", "-");

          switch (tempData.OrderBase.OrderStatus) {
            case -1:
              tempData.OrderBase.OrderStatusDesc = "未知状态"
              break;
            case 0:
              tempData.OrderBase.OrderStatusDesc = "未提交"
              break;
            case 1:
              tempData.OrderBase.OrderStatusDesc = "新订单"
              break;
            case 2:
              tempData.OrderBase.OrderStatusDesc = "已受理"
              break;
            case 3:
              tempData.OrderBase.OrderStatusDesc = "已取消"
              break;
            case 4:
              tempData.OrderBase.OrderStatusDesc = "待出票"
              break;
            case 5:
              tempData.OrderBase.OrderStatusDesc = "已出票"
              break;
            case 6:
              tempData.OrderBase.OrderStatusDesc = "已完成"
              break;
            case 16:
              tempData.OrderBase.OrderStatusDesc = "需补款"
              break;
            case 17:
              tempData.OrderBase.OrderStatusDesc = "不能出票"
              break;
            case 30:
              tempData.OrderBase.OrderStatusDesc = "出票中"
              break;
            case 31:
              tempData.OrderBase.OrderStatusDesc = "已退款"
              break;
            case 32:
              tempData.OrderBase.OrderStatusDesc = "退款中"
              break;
            case 33:
              tempData.OrderBase.OrderStatusDesc = "暂不能出票"
              break;
          }

          baseutil.log(tempData)

          THIS.setData({
            OrderDetail: tempData
          })
        } else {
          THIS.wetoast.toast({
            title: res.data.ResponseMetaInfo.Reason,
            duration: 1000
          })
        }
      })
  },
  getTrainOrderDetail() {
    var THIS = this
    var url = config.getBaseUrl("Train", "GetOrderDetail");

    var Data = new Object();
    Data.OrderNo = this.data.OrderNo

    baseutil.log(url);
    netutil.post(
      url, Data,
      function (res) {
        baseutil.log(res.data);
        if (res.data.ResponseMetaInfo.StatusCode == '0') {
          var tempData = res.data.Data



          var TravelApproveProcess = tempData.TravelApproveOrder.TravelApproveProcess
          var curUserId = app.globalData.TUserInfo.UserID
          for (var i = 0; i < TravelApproveProcess.length; i++) {
            var tempTravelApproveProcess = TravelApproveProcess[i]
            tempTravelApproveProcess.ApproveTime = (tempTravelApproveProcess.ApproveTime == null
              || tempTravelApproveProcess.ApproveTime == '') ? "" : tempTravelApproveProcess.ApproveTime.slice(5, tempTravelApproveProcess.ApproveTime.length - 3).replace("T", ' ').replace("/", "-");

            tempTravelApproveProcess.XingShi = tempTravelApproveProcess.TrueName.substr(0, 1);

            if (tempTravelApproveProcess.ApproveResult == 0) {
              tempTravelApproveProcess.StateText = "待审批";
              tempTravelApproveProcess.curr = true;

            } else if (tempTravelApproveProcess.ApproveResult == 1) {
              tempTravelApproveProcess.StateText = "审批通过";
              tempTravelApproveProcess.curr = false;
            } else {
              tempTravelApproveProcess.StateText = "审批拒绝";
              tempTravelApproveProcess.curr = false;
            }

            TravelApproveProcess[i] = tempTravelApproveProcess
          }
          tempData.TravelApproveOrder.TravelApproveProcess = TravelApproveProcess

          var curUserId = app.globalData.TUserInfo.UserID
          tempData.OrderInfo.isShowBottomBtn = false
          for (var i = 0; i < TravelApproveProcess.length; i++) {
            var tempTravelApproveProcess = TravelApproveProcess[i]
            if (tempTravelApproveProcess.ApproveResult == 0) {
              tempData.OrderInfo.isShowBottomBtn = (tempTravelApproveProcess.UserID == curUserId);
              baseutil.log(tempTravelApproveProcess.UserID + "---" + curUserId + "---" + tempData.OrderInfo.isShowBottomBtn)
              if (tempData.OrderInfo.isShowBottomBtn) {
                break;
              }
            }
          }

          var Passengers = tempData.Passengers
          for (var i = 0; i < Passengers.length; i++) {
            var tempPassengers = Passengers[i]
            tempPassengers.cardText = baseutil.getCardText(tempPassengers.CardType)
            Passengers[i] = tempPassengers
          }
          tempData.Passengers = Passengers

          tempData.OrderInfo.serviceFee = parseFloat(tempData.OrderInfo.SingleOASerFee) + parseFloat(tempData.OrderInfo.SingleCitySecSerFee)

          tempData.OrderInfo.isShowPayButton = tempData.OrderInfo.PayStatus == 0 && tempData.OrderInfo.OrderStatus == 1 && (tempData.OrderInfo.VerifyStatus == 0 || tempData.OrderInfo.VerifyStatus == 2)
          tempData.OrderInfo.OrderTime = tempData.OrderInfo.OrderTime.slice(5, tempData.OrderInfo.OrderTime.length - 3).
            replace("T", ' ').replace("/", "-");
          tempData.OrderInfo.StartTime = tempData.OrderInfo.StartTime.slice(5, tempData.OrderInfo.StartTime.length - 3).
            replace("T", ' ').replace("/", "-");
          tempData.OrderInfo.EndTime = tempData.OrderInfo.EndTime.slice(5, tempData.OrderInfo.EndTime.length - 3).
            replace("T", ' ').replace("/", "-");
          // 新订单 = 1,待出票 = 2,已出票 = 3,已取消 = 4,退款中 = 7,已退款 = 8,不能出票 = 9

          switch (tempData.OrderInfo.OrderStatus) {
            case 1:
              tempData.OrderInfo.OrderStatusDesc = "新订单"
              break;
            case 2:
              tempData.OrderInfo.OrderStatusDesc = "待出票"
              break;
            case 3:
              tempData.OrderInfo.OrderStatusDesc = "已出票"
              break;
            case 4:
              tempData.OrderInfo.OrderStatusDesc = "已取消"
              break;
            case 7:
              tempData.OrderInfo.OrderStatusDesc = "已出票"
              break;
            case 8:
              tempData.OrderInfo.OrderStatusDesc = "已退款"
              break;
            case 9:
              tempData.OrderInfo.OrderStatusDesc = "不能出票"
              break;
          }

          baseutil.log(tempData)

          THIS.setData({
            OrderDetail: tempData
          })
        } else {
          THIS.wetoast.toast({
            title: res.data.ResponseMetaInfo.Reason,
            duration: 1000
          })
        }
      })
  },
  showOrderDetail() {
    if (this.data.OrderType == 1) {
      var showTicketDetail = !this.data.showTicketDetail
      baseutil.log(showTicketDetail)
      this.setData({ showTicketDetail: showTicketDetail })
    } else {
      var showTrainDetail = !this.data.showTrainDetail
      baseutil.log(showTrainDetail)
      this.setData({ showTrainDetail: showTrainDetail })
    }

  },
  agree: function (e) {
    this.setData({
      isShowAgreeDialog: true,
    })
  },
  refuse: function (e) {
    this.setData({
      isShowRefuseDialog: true,
    })
  },
  cancel: function (e) {
    var type = e.currentTarget.dataset.type
    if (type == 1) {
      this.setData({
        isShowAgreeDialog: false,
      })
    } else {
      this.setData({
        isShowRefuseDialog: false,
      })
    }
  },
  confirm: function (e) {
    var type = e.currentTarget.dataset.type

    if (type == 1) {
      this.setData({
        isShowAgreeDialog: false,
      })
    } else {
      this.setData({
        isShowRefuseDialog: false,
      })
    }
    this.goOperate(type);
  },
  goOperate(type) {
    var THIS = this
    var url = config.getBaseUrl("TravelApprove", "DealApprove");
    var Data = new Object();
    Data.ApproveOrderID = this.data.OrderDetail.TravelApproveOrder.OrderBase.ApproveOrderID

    baseutil.log(Data.ApproveOrderID)

    Data.CurrentApproveUser = app.globalData.TUserInfo.UserID
    Data.ApproveResult = type == 1 ? 1 : 2
    Data.Remark = this.data.refuseReason
    Data.ApproveType = 1

    baseutil.log(url);
    netutil.post(
      url, Data,
      function (res) {
        baseutil.log(res.data);
        if (res.data.ResponseMetaInfo.StatusCode == '0') {
          THIS.wetoast.toast({
            title: type == 1 ? '审批已同意!' : '审批已拒绝!',
            duration: 1000
          })
          THIS.getOrderData();
          var pages = getCurrentPages();
          var prePage = pages[pages.length - 2];  //上一个页面
          prePage.refresh();
        } else {
          THIS.wetoast.toast({
            title: res.data.ResponseMetaInfo.Reason,
            duration: 1000
          })
        }
      })
  },
  getRefuseReason(e) {
    var refuseReason = e.detail.value;
    this.setData({ refuseReason: refuseReason })
  },
  cancelOrder() {
    var THIS = this
    var url = null
    var Data = new Object();
    if (this.data.OrderType == 1) {
      url = config.getBaseUrl("FlightApi", "CancelOrder");
      Data.Remark = ""
      Data.UserTypeText = "员工";//员工
      Data.UserNumber = app.globalData.TUserInfo.TrueName;//员工姓名
    } else {
      url = config.getBaseUrl("Train", "CancelOrder");

    }
    Data.UserID = app.globalData.TUserInfo.UserID
    Data.OrderNo = this.data.OrderNo
    Data.SiteID = app.globalData.TUserInfo.SiteID


    baseutil.log(url);
    netutil.post(
      url, Data,
      function (res) {
        baseutil.log(res.data);
        if (THIS.data.OrderType == 1) {
          if (res.data.Data.IsSuccess) {
            var tempData = res.data.Data
            THIS.wetoast.toast({
              title: "订单已取消",
              duration: 1000
            })
            THIS.getOrderData()
          } else {
            THIS.wetoast.toast({
              title: res.data.Data.Message,
              duration: 1000
            })
          }

        } else {
          if (res.data.ResponseMetaInfo.StatusCode == '0') {
            var tempData = res.data.Data
            THIS.wetoast.toast({
              title: "订单已取消",
              duration: 1000
            })
            THIS.getOrderData()
          } else {
            THIS.wetoast.toast({
              title: res.data.ResponseMetaInfo.Reason,
              duration: 1000
            })
          }
        }


      })
  },
  goPay() {
    //    OrderType: '',//订单类别(对应枚举：OrderType:1、国内机票，2、国际机票，3、火车票) 
    var OrderDetail = this.data.OrderDetail
    var OrderType = this.data.OrderType

    // payorderno, money, transactionType, subTransactionType, fromWhere
    var payorderno = OrderType == 1 ? OrderDetail.OrderBase.OrderNO : OrderDetail.OrderInfo.OrderNo
    var money = OrderType == 1 ? OrderDetail.OrderBase.OrderAmount : OrderDetail.OrderInfo.OrderAmount
    var transactionType = OrderType == 1 ? 1 : 5
    var subTransactionType = OrderType == 1 ? 1 : 11
    var fromWhere = OrderType == 1 ? 1 : 0
    baseutil.log("---")
    baseutil.log("payorderno=" + payorderno + ",money=" + money + ",transactionType=" + transactionType + ",subTransactionType=" + subTransactionType + ",fromWhere=" + fromWhere)
    baseutil.log("---")
    payutil.getPayInfo(payorderno,
      money,
      transactionType,
      subTransactionType,
      fromWhere, 1);
  }
})